package com.mmy.fta.cargo.web.response;

import com.ymm.trade.enums.DefaultResponseCode;
import com.ymm.trade.enums.ResponseCode;
import java.io.Serializable;
import java.util.UUID;

/**
 * @author guohao.xie
 * @apiNote
 * @exportable
 * @since 2021/10/21
 */
public class BaseResponse  implements Serializable {

    private String errorMsg;

    /**
     * 成功返回：1
     * 失败返回：0
     */
    private int result;

    private String requestId;

    /**
     * 默认返回码为1：成功
     */
    public BaseResponse() {
        this(DefaultResponseCode.SUCCESS);
    }

    public BaseResponse(int result) {
        this.result = result;
    }

    public BaseResponse(ResponseCode result) {
        this(result.getState(), result.getMsg());
    }

    public BaseResponse(int result, String msg) {
        this.result = result;
        this.errorMsg = msg;
    }

    public void success() {
        this.respond(DefaultResponseCode.SUCCESS);
    }

    public boolean isSuccess() {
        return DefaultResponseCode.SUCCESS.getState() == this.result;
    }

    public void respond(ResponseCode code) {
        this.result = code.getState();
        this.errorMsg = code.getMsg();
    }

    public void respond(int errorCode, String errorMsg) {
        this.result = errorCode;
        this.errorMsg = errorMsg;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public void writeErrorMsgWithResult(String errorMsg) {
        this.result = DefaultResponseCode.RESPONSE_USE_ERROR_MSG.getState();
        this.errorMsg = errorMsg;
    }

    public int getResult() {
        return result;
    }

    public String getRequestId() {
        String str = UUID.randomUUID().toString();
        this.requestId = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
        return this.requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public void setResult(int result) {
        this.result = result;
    }
}
